// pages/charging/charging.js
const SERVER = require("../../interfaces/interface")
const ORDER = require("../../interfaces/order")
const util = require("../../utils/util")
Page({
  /**
   * 页面的初始数据
   */
  data: {
    currentOrder:{
      "front_cars": 17,
      "id": "27",
      "user_id": "48",
      "car_id": "6",
      "mode": "F",
      "apply_kwh": 28,
      "charge_kwh": 38,
      "state": "cupidatat veniam esse",
      "charge_price": 54,
      "service_price": 53,
      "fee": 80,
      "create_time": "2023/06/06 15:20:00",
      "dispatch_time": "2023/06/06 15:20:00",
      "charge_id": "94",
      "start_time": "2023/06/06 15:20:00",
      "finish_time": "2023/06/06 15:20:00",
      "number": 99
    },
    now:"",
  },
  inter:undefined,
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 显示时启动
    if(this.inter===undefined)
    {
      console.log("启动定时器")
      this.inter = setInterval(()=>{
        let now = util.formatTime(new Date())
        console.log(now)
        this.setData({
          currentOrder:ORDER.getCurrentOrder(),
          now:now
        })
      }, 1000)
    }  
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    clearInterval(this.inter)
    console.log("终止")
    this.inter = undefined
  },

  onStopCharging()
  {
    wx.showModal({
      title:"请确认",
      content:"是否取消充电请求",
      success:async(res)=>{
        if(res.confirm){
          
          let order_id = this.data.currentOrder.id
          // 这里还没有获取到订单编号
          wx.showLoading({
            title: '请稍后',
            mask:true
          })
          let result = await SERVER.cancelOrder(order_id)
          wx.hideLoading()
          if(result.status!="success")
          {
            console.log(result)
            wx.showToast({
              title: '取消失败请重试',
              icon:"error"
            })
            return
          }
          else{
            wx.navigateBack()
            wx.showToast({
              title: '取消成功',
            })
          }
        }
      }
    })
  }

})